package lc1993929.com.secret.ld;


import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import lc1993929.com.secret.Config;
import lc1993929.com.secret.utils.MD5Tools;

/**
 * Created by LiuChang on 2017/5/8.
 */

public class MyContacts {

    public static String getContactsJSONString(Context context) {
        Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        String phoneNum;
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject;

        while (cursor.moveToNext()) {
            phoneNum = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            if (phoneNum.charAt(0) == '+' && phoneNum.charAt(1) == '8' && phoneNum.charAt(2) == '6') {
                phoneNum = phoneNum.substring(3);
            }

            jsonObject = new JSONObject();
            try {
                jsonObject.put(Config.KEY_PHONE_MD5, MD5Tools.MD5(phoneNum));
            } catch (JSONException e) {
                e.printStackTrace();
            }

            jsonArray.put(jsonObject);

        }
        return jsonArray.toString();
    }
}
